//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Spam.Rules
{
	/// <summary>
	/// Rates spam factor based on number of occurences of configured forbidden words
	/// </summary>
	public class ForbiddenWordSpamRule : SpamRule
	{
		private static readonly Guid ruleID = new Guid("3bef40fa-8c72-442c-9e40-194b4c129e17");
		private static readonly string resourceFile = "ControlPanelResources.xml";
		private static readonly int defaultPointsPerWord = 5;
		private static readonly string defaultForbiddenWords = "";

		public ForbiddenWordSpamRule()
		{
		}

		public override string Name
		{
			get { return ResourceManager.GetString("spam_ForbiddenWordRule_Name", resourceFile); }
		}

		public override string Description
		{
			get { return ResourceManager.GetString("spam_ForbiddenWordRule_Description", resourceFile); }
		}

		public override Guid RuleID
		{
			get { return ruleID; }
		}

		public override ArrayList GetAvailableSettings()
		{
			ArrayList settings = new ArrayList();
			settings.Add(new RuleSetting(ruleID, "pointsperword", ResourceManager.GetString("spam_ForbiddenWordRule_PointsPerWordDescription", resourceFile), defaultPointsPerWord.ToString()));
			settings.Add(new RuleSetting(ruleID, "forbiddenwords", ResourceManager.GetString("spam_ForbiddenWordRule_ForbiddenWordsDescription", resourceFile), defaultForbiddenWords, 8000) );
			return settings;
		}

		public override int RatePost(Post post, CSPostEventArgs e)
		{
			// Verify that this spam rule should process this post
			if (IsExemptFromSpamRating(post, e))
				return 0;

			// Retrieve setting values
			int pointsPerWord = Globals.SafeInt(this.GetSettingValue("pointsperword"), defaultPointsPerWord);

			string forbiddenWordString = GetSettingValue("forbiddenwords");
			if (forbiddenWordString == null)
				forbiddenWordString = defaultForbiddenWords;

			// If no forbidden words are selected, exit method
			if (forbiddenWordString.Trim().Length <= 0)
				return 0;

			string[] forbiddenWords = forbiddenWordString.Split(';');

			// Count forbidden words
			if (forbiddenWords != null && forbiddenWords.Length > 0)
			{
				int counter = 0;
				foreach (string word in forbiddenWords)
					if (word.Trim().Length > 0)
						counter += SpamRuleHelper.CountBadWord(word.Trim(), post);

				// Score X points for each forbiddenword
				return (counter * pointsPerWord);
			}

			return 0;
		}
	}
}
